package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication.databinding.ActivityGuideBinding;
import com.example.myapplication.databinding.GuidePageBinding;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.annotations.NonNull;

public class GuideActivity extends AppCompatActivity {
    private ActivityGuideBinding binding;
    private List<GuideItem> guideItems;
    private List<View> indicators;
    private Handler autoScrollHandler;
    private static final long AUTO_SCROLL_DELAY = 3000; // 3秒滑动一次
    private boolean isAutoScrolling = true;

    private final Runnable autoScrollRunnable = new Runnable() {
        @Override
        public void run() {
            if (!isAutoScrolling) return;
            
            int currentItem = binding.viewPager.getCurrentItem();
            // 如果不是最后一页，继续自动滑动
            if (currentItem < guideItems.size() - 1) {
                binding.viewPager.setCurrentItem(currentItem + 1, true);
                autoScrollHandler.postDelayed(this, AUTO_SCROLL_DELAY);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        autoScrollHandler = new Handler();
        
        initGuideData();
        setupViewPager();
        setupIndicators();
        setupStartButton();
        startAutoScroll();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startAutoScroll();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopAutoScroll();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopAutoScroll();
    }

    private void startAutoScroll() {
        isAutoScrolling = true;
        autoScrollHandler.postDelayed(autoScrollRunnable, AUTO_SCROLL_DELAY);
    }

    private void stopAutoScroll() {
        isAutoScrolling = false;
        autoScrollHandler.removeCallbacks(autoScrollRunnable);
    }

    private void initGuideData() {
        guideItems = new ArrayList<>();
        guideItems.add(new GuideItem(
            R.drawable.guide_weather,
            "实时天气",
            "随时掌握天气变化，精准预报助您合理安排生活"
        ));
        guideItems.add(new GuideItem(
            R.drawable.guide_forecast,
            "未来预报",
            "查看未来天气趋势，提前规划出行计划"
        ));
        guideItems.add(new GuideItem(
            R.drawable.guide_life,
            "生活指数",
            "贴心的生活指数提醒，让您的生活更加舒适安心"
        ));
    }

    private void setupViewPager() {
        GuideAdapter adapter = new GuideAdapter(guideItems);
        binding.viewPager.setAdapter(adapter);
        
        // 注册页面切换回调
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateIndicators(position);
                if (position == guideItems.size() - 1) {
                    binding.startButton.setVisibility(View.VISIBLE);
                    // 在最后一页停止自动滑动
                    stopAutoScroll();
                } else {
                    binding.startButton.setVisibility(View.GONE);
                    // 不是最后一页时恢复自动滑动
                    startAutoScroll();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // 当用户手动滑动时，暂停自动滑动
                if (state == ViewPager2.SCROLL_STATE_DRAGGING) {
                    stopAutoScroll();
                } else if (state == ViewPager2.SCROLL_STATE_IDLE && 
                         binding.viewPager.getCurrentItem() < guideItems.size() - 1) {
                    // 只有在不是最后一页时才恢复自动滑动
                    startAutoScroll();
                }
            }
        });
    }

    private void setupIndicators() {
        indicators = new ArrayList<>();
        for (int i = 0; i < guideItems.size(); i++) {
            View indicator = new View(this);
            int size = (int) (getResources().getDisplayMetrics().density * 8);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size, size);
            params.setMargins(size, 0, size, 0);
            indicator.setLayoutParams(params);
            indicator.setBackgroundResource(i == 0 ? 
                R.drawable.indicator_selected : R.drawable.indicator_unselected);
            binding.indicatorLayout.addView(indicator);
            indicators.add(indicator);
        }
    }

    private void updateIndicators(int position) {
        for (int i = 0; i < indicators.size(); i++) {
            indicators.get(i).setBackgroundResource(
                i == position ? R.drawable.indicator_selected : R.drawable.indicator_unselected
            );
        }
    }

    private void setupStartButton() {
        binding.startButton.setOnClickListener(v -> {
            // 直接跳转到主页面
            startActivity(new Intent(this, MainActivity.class));
            finish();
        });
    }

    private static class GuideItem {
        int imageRes;
        String title;
        String description;

        GuideItem(int imageRes, String title, String description) {
            this.imageRes = imageRes;
            this.title = title;
            this.description = description;
        }
    }

    private class GuideAdapter extends RecyclerView.Adapter<GuideAdapter.GuideViewHolder> {
        private List<GuideItem> items;

        GuideAdapter(List<GuideItem> items) {
            this.items = items;
        }

        @NonNull
        @Override
        public GuideViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            GuidePageBinding binding = GuidePageBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
            return new GuideViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull GuideViewHolder holder, int position) {
            GuideItem item = items.get(position);
            holder.binding.guideImage.setImageResource(item.imageRes);
            holder.binding.guideTitle.setText(item.title);
            holder.binding.guideDescription.setText(item.description);
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        class GuideViewHolder extends RecyclerView.ViewHolder {
            GuidePageBinding binding;

            GuideViewHolder(GuidePageBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }
        }
    }
} 